﻿Imports System
Imports System.ComponentModel
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
Imports CQRSAzure.Tools

''' <summary>
''' Class to describe an event for code generation
''' </summary>
''' <remarks>
''' Each event must have an unique name
''' </remarks>
<XmlType("eventModel", AnonymousType:=False)>
<DataContract(Name:=NameOf(EventModel))>
<Serializable>
Public NotInheritable Class EventModel
    Implements INotifyPropertyChanged
    Implements IEquatable(Of EventModel)

    <DataMember(Name:=NameOf(Name))>
    Private ReadOnly m_name As String
    ''' <summary>
    ''' The unique name by which the event is known
    ''' </summary>
    <XmlAttribute(AttributeName:="EventName")>
    Public ReadOnly Property Name As String
        Get
            Return m_name
        End Get
    End Property

#Region "Serialisation"

    Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext)
        If (info IsNot Nothing) Then
            info.AddValue(NameOf(Name), m_name)
        End If
    End Sub

#End Region

#Region "Property Change Notification"
    Public Sub OnPropertyChanged(ByVal propertyName As String)

        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))

    End Sub
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
#End Region

#Region "Equality"

    Public Overrides Function Equals(obj As Object) As Boolean

        If (TypeOf (obj) Is EventModel) Then
            Return EqualsEventModel(obj)
        Else
            Return False
        End If

    End Function

    Public Overloads Function EqualsEventModel(other As EventModel) As Boolean Implements IEquatable(Of EventModel).Equals

        If (other IsNot Nothing) Then
            Return m_name.Equals(other.Name)
        Else
            Return False
        End If

    End Function

#End Region

#Region "Public constructors"
    ''' <summary>
    ''' Empty constructor for serialisation
    ''' </summary>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Create a new event model with the given name
    ''' </summary>
    ''' <param name="InitialEventName">
    ''' The name to use for the event
    ''' </param>
    Public Sub New(ByVal InitialEventName As String)
        m_name = InitialEventName
    End Sub

    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        If (info IsNot Nothing) Then
            m_name = info.GetString(NameOf(Name))
        End If
    End Sub

#End Region

End Class
